import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './styles/element-ui-fix.scss'
import './styles/global.scss'
import i18n from './i18n'
import './utils/filters'
import ModuleManager from './modules/ModuleManager'
import MessageControl from './utils/messageControl'

Vue.config.productionTip = false

Vue.use(ElementUI)
Vue.use(MessageControl) // Install message control after ElementUI
Vue.use(ModuleManager)

const app = new Vue({
  router,
  store,
  i18n,
  render: h => h(App),
  created() {
    // 初始化默认标签页（仪表盘）
    this.$store.commit('ui/ADD_TAB', {
      id: 'dashboard',
      title: this.$i18n.t('menu.dashboard'),
      route: '/dashboard',
      closable: false
    })
    this.$store.commit('ui/SET_ACTIVE_TAB', 'dashboard')
  }
}).$mount('#app')

window.addEventListener('beforeunload', () => {
  store.dispatch('saveAllConfigs')
})

// Electron IPC communication
if (window.require) {
  const { ipcRenderer } = window.require('electron')
  
  ipcRenderer.on('menu-new-connection', () => {
    store.dispatch('ui/showNewConnectionDialog')
  })

  ipcRenderer.on('menu-import-config', () => {
    store.dispatch('config/importConfig')
  })

  ipcRenderer.on('menu-export-config', () => {
    store.dispatch('config/exportConfig')
  })

  ipcRenderer.on('menu-open-module', (event, module) => {
    store.dispatch('modules/openModule', module)
  })

  ipcRenderer.on('menu-open-settings', (event, section) => {
    store.dispatch('ui/openSettings', section)
  })
}

export default app